package com.mail.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.api.*;
import com.mail.model.db.*;
import com.mail.model.dto.ProductParamDto;
import com.mail.model.dto.QueryProductDto;
import com.mail.model.vo.PageResult;
import lombok.val;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PmsProductService {

	@DubboReference
	private PmsProductApi pmsProductApi;


	// 新建商品
	public void createProduct(ProductParamDto paramDto) {
		// 存入商品
		PmsProduct pmsProduct = new PmsProduct();
		BeanUtils.copyProperties(paramDto,pmsProduct);

		pmsProductApi.saveProduct(pmsProduct);
		// 封装商品会员价格表
		List<PmsMemberPrice> pmsMemberPriceList = paramDto.getMemberPriceList();
		for (PmsMemberPrice pmsMemberPrice : pmsMemberPriceList) {
			// 添加商品价格
			PmsMemberPrice memberPrice = new PmsMemberPrice();
			BeanUtils.copyProperties(pmsMemberPrice, memberPrice);
			pmsProductApi.saveMemberPrice(memberPrice);
		}

		// 封装优选专区和产品关系表
		List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = paramDto.getPrefrenceAreaProductRelationList();
		for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
			CmsPrefrenceAreaProductRelation prefrenceAreaProductRelation = new CmsPrefrenceAreaProductRelation();
			BeanUtils.copyProperties(cmsPrefrenceAreaProductRelation, prefrenceAreaProductRelation);
			pmsProductApi.savePrefAreaProRelation(prefrenceAreaProductRelation);
		}

		// 封装存储产品参数信息的表
		List<PmsProductAttributeValue> productAttributeValueList = paramDto.getProductAttributeValueList();
		for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
			PmsProductAttributeValue productAttributeValue = new PmsProductAttributeValue();
			BeanUtils.copyProperties(pmsProductAttributeValue, productAttributeValue);
			pmsProductApi.saveProAttValue(productAttributeValue);
		}

		// 封装产品满减表(只针对同商品)
		List<PmsProductFullReduction> productFullReductionList = paramDto.getProductFullReductionList();
		for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
			PmsProductFullReduction productFullReduction = new PmsProductFullReduction();
			BeanUtils.copyProperties(pmsProductFullReduction, productFullReduction);
			pmsProductApi.saveProFullReduction(productFullReduction);
		}

		// 封装产品阶梯价格表(只针对同商品)
		List<PmsProductLadder> productLadderList = paramDto.getProductLadderList();
		for (PmsProductLadder pmsProductLadder : productLadderList) {
			PmsProductLadder productLadder = new PmsProductLadder();
			BeanUtils.copyProperties(pmsProductLadder, productLadder);
			pmsProductApi.saveProLadder(productLadder);
		}

		// 封装sku的库存
		List<PmsSkuStock> skuStockList = paramDto.getSkuStockList();
		for (PmsSkuStock pmsSkuStock : skuStockList) {
			PmsSkuStock skuStock = new PmsSkuStock();
			BeanUtils.copyProperties(pmsSkuStock, skuStock);
			pmsProductApi.saveSkuStock(skuStock);
		}

		// 封装专题商品关系表
		List<CmsSubjectProductRelation> subjectProductRelationList = paramDto.getSubjectProductRelationList();
		for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
			CmsSubjectProductRelation subjectProductRelation = new CmsSubjectProductRelation();
			BeanUtils.copyProperties(cmsSubjectProductRelation, subjectProductRelation);
			pmsProductApi.saveSubProRelation(subjectProductRelation);
		}

		// 往product_attribute_category表中存入对应属性数量和参数数量
		Long productAttributeCategoryId = paramDto.getProductAttributeCategoryId();
		pmsProductApi.saveCount(productAttributeCategoryId);

	}

	// 查询商品
	public PageResult queryProductList(QueryProductDto queryProductDto) {
		PageResult pageResult = new PageResult();
		Page<PmsProduct> page = pmsProductApi.queryProductList(queryProductDto);
		pageResult.setList(page.getRecords());
		pageResult.setPageNum(page.getCurrent());
		pageResult.setPageSize(page.getSize());
		pageResult.setTotalPage(page.getPages());
		pageResult.setTotal(page.getTotal());
		return pageResult;
	}

	// 根据商品名称或货号模糊查询
	public List<PmsProduct> queryProductByKeyword(String keyword) {
		List<PmsProduct> pmsProductList = pmsProductApi.queryProductByKeyword(keyword);
		return pmsProductList;
	}

	// 批量修改删除状态
	public void deleteStatus(Integer deleteStatus, List<Integer> ids) {
		pmsProductApi.deleteStatus(deleteStatus, ids);
	}

	// 批量设为新品
	public void newStatus(Integer newStatus, List<Integer> ids) {
		pmsProductApi.newStatus(newStatus, ids);
	}

	// 批量上下架商品
	public void publishStatus(Integer publishStatus, List<Integer> ids) {
		pmsProductApi.publishStatus(publishStatus, ids);
	}

	// 批量推荐商品
	public void recommendStatus(Integer recommendStatus, List<Integer> ids) {
		pmsProductApi.recommendStatus(recommendStatus, ids);
	}

	// 批量修改审核状态
	public void verifyStatus(String detail, Integer verifyStatus, List<Integer> ids) {
		pmsProductApi.verifyStatus(detail, verifyStatus, ids);
	}

	// 更新商品
	public void updateProduct(Integer id, ProductParamDto productParamDto) {
		Map<String, Object> map = new HashMap<>();
		PmsProduct pmsProduct = new PmsProduct();
		BeanUtils.copyProperties(productParamDto,pmsProduct);
		map.put("pmsProduct",pmsProduct);

		// 封装商品会员价格对象
		if (productParamDto.getMemberPriceList() != null && productParamDto.getMemberPriceList().size() != 0) {
			PmsMemberPrice memberPrice = new PmsMemberPrice();
			BeanUtils.copyProperties(productParamDto.getMemberPriceList().get(0), memberPrice);
			map.put("memberPrice", memberPrice);
		}
		// 封装优选专区和产品关系对象
		if (!CollUtil.isEmpty(productParamDto.getPrefrenceAreaProductRelationList())){

		CmsPrefrenceAreaProductRelation prefrenceAreaProductRelation = new CmsPrefrenceAreaProductRelation();
		BeanUtils.copyProperties(productParamDto.getPrefrenceAreaProductRelationList().get(0), prefrenceAreaProductRelation);
		map.put("prefrenceAreaProductRelation", prefrenceAreaProductRelation);
		}
		// 封装存储产品参数信息的对象
		if (CollUtil.isNotEmpty(productParamDto.getProductAttributeValueList())){

		PmsProductAttributeValue productAttributeValue = new PmsProductAttributeValue();
		BeanUtils.copyProperties(productParamDto.getProductAttributeValueList().get(0), productAttributeValue);
		map.put("productAttributeValue", productAttributeValue);
		}
		// 封装产品满减对象(只针对同商品)
		if (CollUtil.isNotEmpty(productParamDto.getProductFullReductionList())){

		PmsProductFullReduction productFullReduction = new PmsProductFullReduction();
		BeanUtils.copyProperties(productParamDto.getProductFullReductionList().get(0), productFullReduction);
		map.put("productFullReduction", productFullReduction);
		}
		// 封装产品阶梯价格对象(只针对同商品)
		if (CollUtil.isNotEmpty(productParamDto.getProductLadderList())){

		PmsProductLadder productLadder = new PmsProductLadder();
		BeanUtils.copyProperties(productParamDto.getProductLadderList().get(0), productLadder);
		map.put("productLadder", productLadder);
		}
		// 封装sku的库存对象
		if (CollUtil.isNotEmpty(productParamDto.getSkuStockList())){

		PmsSkuStock skuStock = new PmsSkuStock();
		BeanUtils.copyProperties(productParamDto.getSkuStockList().get(0), skuStock);
		map.put("skuStock", skuStock);
		}
		// 封装专题商品关系对象
		if(CollUtil.isNotEmpty(productParamDto.getSubjectProductRelationList())) {
			CmsSubjectProductRelation subjectProductRelation = new CmsSubjectProductRelation();
			BeanUtils.copyProperties(productParamDto.getSubjectProductRelationList().get(0), subjectProductRelation);
			map.put("subjectProductRelation", subjectProductRelation);
		}
		pmsProductApi.updateProduct(id, map);
	}

	// 根据商品id获取商品编辑信息
	public ProductParamDto queryByProductId(Integer productId) {
		ProductParamDto productParamDto = pmsProductApi.queryByProductId(productId);
		return productParamDto;
	}
}